﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace SalesOrderBackBoneProject
{
    public class CustomerController : ApiController
    {
        private AdventureWorksLTEntities ade;
        public CustomerController()
        {
            ade = new AdventureWorksLTEntities();
        }
        // GET api/<controller>
        public IEnumerable<CustomerShort> Get()
        {
            return GetCustomerShorts().Take(5).AsEnumerable();
        }

        public CustomerShort Get(int id)
        {
            return GetCustomerShorts().SingleOrDefault();
        }

        private IQueryable<CustomerShort> GetCustomerShorts()
        {
            //return new List<CustomerShort>
            //{
            //    new CustomerShort
            //    {
            //        Id = 0,
            //        FirstName = "Peter",
            //        LastName = "Vogel"
            //    },
            //    new CustomerShort
            //    {
            //        Id = 1,
            //        FirstName = "Jan",
            //        LastName = "Vogel"
            //    },
            //    new CustomerShort
            //    {
            //        Id = 2,
            //        FirstName = "Jason",
            //        LastName = "Van deVelde",
            //    }
            //}.AsQueryable();

            return from c in ade.Customers
                   select new CustomerShort
                   {
                       Id = c.CustomerID,
                       FirstName = c.FirstName,
                       LastName = c.LastName,
                   };
        }

        // POST api/<controller>
        public void Post([FromBody]string value)
        {
        }

        // PUT api/<controller>/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/<controller>/5
        public void Delete(int id)
        {
        }
    }
    public class CustomerShort
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string FullName
        {
            get { return LastName + ", " + FirstName; }
        }
    }
}